import { _decorator, Sprite, Node, UITransform, tween } from 'cc';
import { EntityTypeEnum, G, IVec2, StateTypeEnum } from '../../Common';
import { createUINode } from '../../Utils';
import { EntityManager } from '../../Base/EntityManager';
import { ExplosionStateMachine } from './ExplosionStateMachine';
const { ccclass, property } = _decorator;

@ccclass('ExplosionManager')
export class ExplosionManager extends EntityManager {

    body: Node
    type: EntityTypeEnum
    init() {

        this.type = EntityTypeEnum.explosion
        this.body = createUINode('body')
        this.body.parent = this.node

        const sprite = this.body.addComponent(Sprite)
        sprite.trim = false
        sprite.sizeMode = Sprite.SizeMode.CUSTOM

        const uiTransform = this.body.getComponent(UITransform)
        uiTransform.setContentSize(G.ExplosionEntitySize.w, G.ExplosionEntitySize.h)

        this.fsm = this.addComponent(ExplosionStateMachine)
        this.fsm.init(this)

    }

    reset(position: IVec2) {

        tween(this.node)
            .delay(0)
            .call(() => {
                this.state = StateTypeEnum.idle
            })
            .start()

        this.node.setPosition(position.x, position.y)
    }
}